var appid = 'wx88b04e459d594722'
var appSecret = '8546c3828903b53f9d1fd3527d063dbd'
var app = getApp()
var domain = app.data.domain
var userModel=require("../model/userModel.js")
var loginUtil=require("loginUtil.js")
// 判断用户是否已经授权登陆
function isLogin(isToLogin) {
  var user = app.data.user
  return user && user.openid
}
function toLogin(){
  wx.navigateTo({
    url: '/pages/login/login',
  })
}
//获取用户资料
function getUserInfo(cb) {
  var user = app.data.user
  if(isLogin()){
    getUser(user.openid,cb)
  }else{
    loginUtil.getOpenId(res=>{
      getUser(res, cb)
      })
  }

}
function getUser(openid,cb){
  userModel.getUserInfo(openid, res => {
    if (res.status == 0)  {
      app.data.user = res.data.customer
      wx.setStorageSync("user", res.data.customer)
      wx.setStorageSync("sessionId", res.data.sessionId)
      if (cb) cb(res.data)
    }
  })
}

module.exports = {
  isLogin: isLogin,
  toLogin: toLogin,
  getUserInfo:getUserInfo,
 
}